package hackathon.light.up;

import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.Log;

public class SpliterT extends Element {

	private boolean visited;

	public SpliterT(int row, int col, LightDirection dir) {
		super(row, col, dir);
		
	}
	@Override
	public void reset() {
		allowedDirections.clear();
		super.allowedDirections.add(orientation);
		lightSources.clear();
		super.lightSources.add(new LightSource(row, col, LightDirection.SE, 2));
		super.lightSources.add(new LightSource(row, col, LightDirection.SE, 2));
		super.lightSources.get(0).flagOn = false;
		super.lightSources.get(1).flagOn = false;
		super.flagOnScene = true;
		visited = false;
	}

	@Override
	public void update(LightSource lightSource) {
		// TODO Auto-generated method stub
		if (flagOnScene) {
			if (!visited) {

				if (allowedDirections.get(0) == lightSource.direction) {
					visited = true;
					for (int j = 0; j < lightSources.size(); j++) {
						if (!lightSources.get(j).flagOn) {
							lightSources.get(j).flagOn = true;
							lightSources.get(j).color_id = lightSource.color_id;
							lightSources.get(j).direction = LightDirection
									.values()[(allowedDirections.get(0)
									.ordinal() + 2 + (j % 2) * 4) % 8];

							int next_col = col
									+ MapDirection.getNextCol(lightSources
											.get(j).direction);
							int next_row = row
									+ MapDirection.getNextRow(lightSources
											.get(j).direction);
							while (Grid.elementMatrix[next_row + 1][next_col + 1] == null) {
								next_col += MapDirection
										.getNextCol(lightSources.get(j).direction);
								next_row += MapDirection
										.getNextRow(lightSources.get(j).direction);
							}
							lightSources.get(j).drawLight(next_row, next_col);
							Grid.elementMatrix[next_row + 1][next_col + 1]
									.update(lightSources.get(j));
						}
					}
				}
			}
		}
	}

}
